package com.neolix.watch;

import android.graphics.Point;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.neolix.watch.adapter.SportRecordAdapter;
import com.neolix.watch.adapter.WeatherAdapter;
import com.neolix.watch.util.ErrorPopWindowsUtil;
import com.neolix.watch.util.SharedPreferencesUtil;
import com.neolix.watch.view.StatisticsCircleView;
import com.neolix.watch.view.TestView;

import org.joda.time.DateTime;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static com.google.common.primitives.Ints.asList;
import static com.neolix.watch.util.ErrorPopWindowsUtil.createPopWindowAndShowAt;

public class StatisticsActivity extends NeolixNaviagationBaseActivity {

    private StatisticsCircleView circleView;
    private GridView gridView;
    private boolean hadCreateGridView;
    private SportRecordAdapter adapter;
    private TestView curveView;
    private View animationView;
    private int screenWidth;
    private List<Integer> stepsList;

    public StatisticsActivity() {
        super(R.layout.activity_statistics);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                lp.setMargins(msg.arg1, 0, 0, 0);
                animationView.setLayoutParams(lp);
                if (msg.arg1 == screenWidth - 1) {
                    Point maxPopWindowPoint = curveView.getMaxPopWindowPoint();
                    ErrorPopWindowsUtil.createPointPopWindowAndShowAt(R.drawable.ic_best_point,
                            curveView, maxPopWindowPoint.x, maxPopWindowPoint.y);
                    createPopWindowAndShowAt(Collections.max(stepsList).toString(),
                            curveView, maxPopWindowPoint.x, maxPopWindowPoint.y, -60);
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCenterImageResource(R.drawable.ic_logo);

        circleView = (StatisticsCircleView) findViewById(R.id.statistics_circle_view);
        gridView = (GridView) findViewById(R.id.sport_gridView);
        curveView = (TestView) findViewById(R.id.curve_view);
        animationView = findViewById(R.id.animation_view);

        initCircleView();
        initListener();

        startAnimation();
        stepsList = asList(0, 0, 0, 0, 0, 0, SharedPreferencesUtil.getCurrentSteps());
        curveView.setData(stepsList);
    }

    private void startAnimation() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                DisplayMetrics dm = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(dm);
                screenWidth = dm.widthPixels;

                for (int i = 0; i < screenWidth; i++) {
                    Message msg = new Message();
                    msg.what = 1;
                    msg.arg1 = i;
                    handler.sendMessage(msg);
                    try {
                        Thread.sleep(4);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        circleView.stopAnimation();
    }

    private void initCircleView() {
        circleView.setGoalStep(10000);
        circleView.setCurrentStepValue(SharedPreferencesUtil.getCurrentSteps());
    }

    private void initListener() {
        ViewTreeObserver vto = gridView.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                if (hadCreateGridView) {
                    return true;
                }
                int itemHeight = gridView.getHeight();
                adapter = new SportRecordAdapter(getApplicationContext(), itemHeight);
                adapter.bindListView(gridView);
                loadDateTimeInfo();
                hadCreateGridView = true;
                return true;
            }
        });

        curveView.setOnSelectListener(new TestView.OnSelectListener() {
            @Override
            public void onSelect(int goalSteps, int currentSteps) {
                circleView.setGoalStep(goalSteps);
                circleView.setCurrentStepValue(currentSteps);
            }
        });
    }

    private void loadDateTimeInfo() {
        DateTime nowTime = DateTime.now();
        List<DateTime> dateTimeList = new ArrayList<DateTime>();
        dateTimeList.add(nowTime.minusDays(6));
        dateTimeList.add(nowTime.minusDays(5));
        dateTimeList.add(nowTime.minusDays(4));
        dateTimeList.add(nowTime.minusDays(3));
        dateTimeList.add(nowTime.minusDays(2));
        dateTimeList.add(nowTime.minusDays(1));
        dateTimeList.add(nowTime);

        adapter.setData(dateTimeList);
    }
}
